<?php

namespace App\Http\Resources;

use App\Traits\BaseResource;
use Illuminate\Http\Resources\Json\Resource;

class Item extends Resource {
	use BaseResource;

	private function toIndex($request) {
		return $this->filterFields([
			'id' => $this->id,
			'cate_id' => $this->cate_id,
			'title' => $this->title,
			'briefly' => $this->briefly,
			'status' => $this->status,
			'see_nums' => $this->see_nums,
			'allow_comment' => $this->allow_comment,
			'cate_name' => $this->whenLoaded('cate', function () {
				return $this->cate->cate_name;
			}),
			'tags' => $this->whenLoaded('tags', function () {
				return $this->tags;
			}),
			'approve_nums' => $this->approve_nums,
			'comment_nums' => $this->comment_nums,
			'created_at' => $this->created_at,
		]);
	}

	private function toTopic($request) {
		return $this->filterFields([
			'id' => $this->id,
			'cate_id' => $this->cate_id,
			'title' => $this->title,
			'briefly' => $this->briefly,
			'status' => $this->status,
			'reprint' => $this->reprint,
			'see_nums' => $this->see_nums,
			'allow_comment' => $this->allow_comment,
			'content' => $this->content,
			'type' => $this->type,
			'cate_name' => $this->whenLoaded('cate', function () {
				return $this->cate->cate_name;
			}),
			'tags' => $this->whenLoaded('tags', function () {
				return $this->tags;
			}),
			'approves' => $this->whenLoaded('approves', function () {
				return $this->approves;
			}),
			'approveing' => $this->whenLoaded('approves', function () {
				$user = \Auth::user();
				return $user ? $user->isApproveing($this->id) : false;
			}),
			'comments' => $this->whenLoaded('comments', function () {
				return (new CommentCollection($this->comments->load('user', 'reply')))->type('index');
			}),
			'created_at' => $this->created_at,
		]);
	}

	private function toEdit($request) {
		return $this->filterFields([
			'id' => $this->id,
			'cate_id' => $this->cate_id,
			'title' => $this->title,
			'briefly' => $this->briefly,
			'status' => $this->status,
			'reprint' => $this->reprint,
			'see_nums' => $this->see_nums,
			'content' => $this->content,
			'type' => $this->type,
			'top' => $this->top,
			'allow_comment' => $this->allow_comment,
			'tags' => $this->whenLoaded('tags', function () {
				return array_column($this->tags->toArray(), 'tag_id');
			}),
		]);
	}

	private function toDefault($request) {
		return $this->filterFields([
			'id' => $this->id,
			'cate_id' => $this->cate_id,
			'title' => $this->title,
			'briefly' => $this->briefly,
			'status' => $this->status,
			'see_nums' => $this->see_nums,
			'top' => $this->top,
			'allow_comment' => $this->allow_comment,
			'cate_name' => $this->whenLoaded('cate', function () {
				return $this->cate->cate_name;
			}),
			'tags' => $this->whenLoaded('tags', function () {
				return $this->tags;
			}),
			'approve_nums' => $this->approve_nums,
			'comment_nums' => $this->comment_nums,
			'created_at' => $this->created_at,
			'updated_at' => $this->updated_at,
		]);
	}
}
